# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=coin-or-cbc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.10.12
pkgrel=1
pkgdesc="COIN-OR Branch-and-Cut solver. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/coin-or/Cbc'
msys2_references=(
  'archlinux: coin-or-cbc'
)
license=('spdx:EPL-2.0')
groups=("${MINGW_PACKAGE_PREFIX}-coin-or")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-coin-or-asl"
         "${MINGW_PACKAGE_PREFIX}-coin-or-cgl"
         "${MINGW_PACKAGE_PREFIX}-coin-or-clp"
         "${MINGW_PACKAGE_PREFIX}-coin-or-coinutils"
         "${MINGW_PACKAGE_PREFIX}-coin-or-osi"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-coin-or-buildtools"
             "${MINGW_PACKAGE_PREFIX}-nauty")
source=("${url}/archive/releases/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('9ed71e4b61668462fc3794c102e26b4bb01a047efbbbcbd69ae7bde1f04f46a8')

prepare() {
  cp -r ${MINGW_PREFIX}/share/coin-or-buildtools/BuildTools Cbc-releases-${pkgver}/Cbc

  cd Cbc-releases-${pkgver}/Cbc

  ACLOCAL="aclocal -I${srcdir}/Cbc-releases-${pkgver}/Cbc/BuildTools" \
  autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  export lt_cv_deplibs_check_method='pass_all'

  CFLAGS+=" -Wno-implicit-function-declaration" \
  CXXFLAGS+=" -Wno-register" \
  ../"Cbc-releases-${pkgver}"/Cbc/configure \
    --prefix="${MINGW_PREFIX}" \
    --enable-shared \
    --disable-static \
    --enable-dependency-linking \
    --enable-cbc-parallel \
    --with-pthreadsw32-lib="-lpthread" \
    --with-pthreadsw32-incdir=${MINGW_PREFIX}/include \
    --with-nauty-lib=${MINGW_PREFIX}/lib/libnauty.a \
    --with-nauty-incdir=${MINGW_PREFIX}/include/nauty \
    ac_cv_c_compiler_gnu=yes

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  make check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make install DESTDIR="${pkgdir}"

  # Remove nauty from linker flags in pc file, it is statically compiled
  sed -i -e "s|${MINGW_PREFIX}/lib/libnauty.a||g" "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/cbc.pc

  install -Dm644 "${srcdir}/Cbc-releases-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
